package com.itbaizhan.shopping_goods_service.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itbaizhan.shopping_common.exception.BusException;
import com.itbaizhan.shopping_common.pojo.ProductType;
import com.itbaizhan.shopping_common.result.CodeEnum;
import com.itbaizhan.shopping_common.service.ProductTypeService;
import com.itbaizhan.shopping_goods_service.mapper.ProductTypeMapper;
import io.netty.util.internal.StringUtil;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author spf
 * @date 2023/12/18
 * @time 13:37
 */

// 产品类型实现类
@Service
@Component
@Transactional
public class ProductTypeServiceImpl implements ProductTypeService {

    @Autowired
    private ProductTypeMapper productTypeMapper;

    @Override
    public void add(ProductType productType) {
        // 查询父类型等级
        ProductType productTypeParent = productTypeMapper.selectById(productType.getParentId());
        // 如果没有父类型，它的等级是 1
        if(productTypeParent == null){
            productType.setLevel(1);
        }else if(productTypeParent.getLevel() < 3){
            productType.setLevel(productTypeParent.getLevel() + 1);
        } else if (productTypeParent.getLevel() >= 3) {
            throw new BusException(CodeEnum.ADD_PRODUCTTYPE_ERROR);
        }
        productTypeMapper.insert(productType);
    }

    @Override
    public void update(ProductType productType) {
        // 查询父类型等级
        ProductType productTypeParent = productTypeMapper.selectById(productType.getParentId());
        // 如果没有父类型，它的等级是 1
        if(productTypeParent == null){
            productType.setLevel(1);
        }else if(productTypeParent.getLevel() < 3){
            productType.setLevel(productTypeParent.getLevel() + 1);
        } else if (productTypeParent.getLevel() >= 3) {
            throw new BusException(CodeEnum.ADD_PRODUCTTYPE_ERROR);
        }
        productTypeMapper.updateById(productType);
    }

    @Override
    public void delete(Long id) {
        // 查看是否有子类型
        QueryWrapper<ProductType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parentId", id);
        List<ProductType> productTypeList = productTypeMapper.selectList(queryWrapper);
        if(productTypeList != null && productTypeList.size() > 0){
            throw new BusException(CodeEnum.DELETE_PRODUCTTYPE_ERROR);
        }
        productTypeMapper.deleteById(id);
    }

    @Override
    public ProductType findById(Long id) {
        return productTypeMapper.selectById(id);
    }

    @Override
    public Page<ProductType> search(ProductType productType, int page, int size) {
        QueryWrapper<ProductType> queryWrapper = new QueryWrapper<>();
        if(productType != null){
            // 根据类型名模糊查询
            if(!StringUtil.isNullOrEmpty(productType.getName())){
                queryWrapper.like("name", productType.getName());
            }
            // 根据上一级id查询
            if (productType.getParentId() != null){
                queryWrapper.eq("parentId", productType.getParentId());
            }
        }
        return productTypeMapper.selectPage(new Page<ProductType>(page, size), queryWrapper);
    }

    @Override
    public List<ProductType> findProductType(ProductType productType) {
        QueryWrapper<ProductType> queryWrapper = new QueryWrapper<>();
        if(productType != null){
            // 根据类型名模糊查询
            if(!StringUtil.isNullOrEmpty(productType.getName())){
                queryWrapper.like("name", productType.getName());
            }
            // 根据上一级id查询
            if (productType.getParentId() != null){
                queryWrapper.eq("parentId", productType.getParentId());
            }
        }
        return productTypeMapper.selectList(queryWrapper);
    }
}
